# -*- coding: utf8 -*-
# 微博超话签到
import requests
import re
from urllib.parse import urlparse
import time

# 获取超话列表
def getChaohuaList(cookie, sinceId):
    print(cookie)
    url = "https://m.weibo.cn/api/container/getIndex?containerid=100803_-_page_my_follow_super"
    if sinceId != '':
        url = url + "&since_id=%s" % sinceId
    headers = {
        'content-type':	'application/json; charset=utf-8',
        'vary':	'Accept-Encoding',
'x-powered-by':	'PHP/7.2.1',
'x-log-uid':	'5308683409',
'content-security-policy':	'upgrade-insecure-requests',
'proc_node':	'mweibo-10-77-40-36.yz.intra.weibo.cn',
'content-encoding':	'gzip',
'ssl_node':	'ssl-014.mweibo.xxg.intra.weibo.cn',
'lb':	'111.13.134.132',
'cache-control':	'no-cache',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
        'Sec-Fetch-Mode': 'cors',
        'Accept': '*/*',
        'Sec-Fetch-Site': 'cross-site',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'Cookie': cookie,
    }
    session = requests.session()
    response = session.get(url, headers=headers)
    respJson = response.json()
    return respJson

# 解析超话数据


def resolveChaohua(cardGroup):
    chaohuaList = []
    for card in cardGroup:
        if card['card_type'] == 8:
            scheme = card['scheme']
            query = urlparse(scheme).query
            parmsList = query.split('&')
            containerid = ''
            for parm in parmsList:
                r = parm.split('=')
                if r[0] == 'containerid':
                    containerid = r[1]
                    break
            chaohua = {
                'title_sub': card['title_sub'],
                'containerid': containerid
            }
            chaohuaList.append(chaohua)
    return chaohuaList

# 超话签到


def signin(cookie, item):
    url = "https://weibo.com/p/aj/general/button?api=http://i.huati.weibo.com/aj/super/checkin&id=%s" % item['containerid']
    headers = {
        'Cookie': cookie
    }
    session = requests.Session()
    response = session.get(url, headers=headers)
    respJson = response.json()
    print(respJson)
    if 'code' in respJson:
        if respJson['code'] == '100000':
            print("%s签到成功!" % item['title_sub'])
            print("返回码为%s" % respJson['code'])
            return "success"
        else:
            print("%s签到失败!" % item['title_sub'])
            print("返回码为%s" % respJson['code'])
            return "error"
    else:
        print("%s签到失败!" % item['title_sub'])
        print("没有返回码（♯▼皿▼）")
        return "error"

def main_handler(event, context):
    /# 注意这里的cookie为访问上面超话列表接口 复制restful工具内的cookie
    tempCookie = ''
    # 获取超话列表
    chaohuaList = []
    sinceId = ""
    isBreak = True
    while isBreak:
        respJson = getChaohuaList(tempCookie, sinceId)
        print(respJson)
        cardlistInfo = respJson['data']['cardlistInfo']
        cardGroup = respJson['data']['cards'][0]['card_group']
        chaohuaList = chaohuaList + resolveChaohua(cardGroup)
        if 'since_id' in cardlistInfo:
            sinceId = cardlistInfo['since_id']
        else:
            print("获取超级话题列表结束...准备开始签到")
            break
    # 循环遍历 进行签到
    allres = ""
    for item in chaohuaList:
        print("-------------------")
        print("准备签到%s" % item['title_sub'])
        signres = signin(tempCookie, item)
        allres += item['title_sub'] + signres + ' \n'
        print("-------------------")
        time.sleep(1)
    # 这里是发送微信结果的  server酱
    requests.get('https://sc.ftqq.com/***********.send?text=' + '微博超话签到完成'+time.strftime('%Y.%m.%d',time.localtime(time.time())) +'&desp='+ allres)